package com.moly.websocketone.handler;

import com.moly.websocketone.annotation.ArgumentAnno;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.annotation.Annotation;

/**
 * @Description:  监测 前台 传入参数
 * @Author: m01
 * @Date: created at  2018/11/16  11:24
 * @Modified By:
 */
public class ArgumentHandler implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {

        System.out.println("-- ArgumentHandler  supportsParameter --");
        methodParameter.hasParameterAnnotation(ArgumentAnno.class);
        Annotation anon=methodParameter.getParameterAnnotation(ArgumentAnno.class);
        System.out.println(((ArgumentAnno) anon).value());
        System.out.println(((ArgumentAnno) anon).required());
        return true;
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter,
                                  ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest,
                                  WebDataBinderFactory webDataBinderFactory) throws Exception {
        System.out.println("-- ArgumentHandler  supportsParameter --");
        String aes = getRequestBody(nativeWebRequest);
        return "moly";
    }

    private String getRequestBody(NativeWebRequest webRequest) {
        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        String jsonBody = (String) servletRequest.getParameter("aes");
        String jsonBody1 = (String) servletRequest.getParameter("aes2");
        return jsonBody;

    }
}
